package com.jiaquyun.jcps.mode.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.jiaquyun.jcps.mode.BaseViewModel
import com.jiaquyun.jcps.entity.*
import com.jiaquyun.jcps.mode.manager.OrderManager
import com.jiaquyun.jcps.utils.AMapLocationUtils
import com.jiaquyun.jcps.utils.HttpWebSocketUtils
import com.lemlin.network.HttpServiceData
import com.lemlin.network.HttpServiceResponse
import com.lemlin.network.ViewModelSubscribe
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit


class MainViewModel(application: Application) : BaseViewModel(application) {

    private val orderManager: OrderManager = OrderManager()


    val baseReceivingLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()
    val baseWithdrawalLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()
    val baseServiceLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()
    val baseCallPhoneLiveData = MutableLiveData<String>()

    val orderTabLiveData = MutableLiveData<HttpServiceResponse<OrderTabResponse>>()
    val orderPickupLiveData = MutableLiveData<HttpServiceResponse<OrderListResponse>>()
    val orderGiveLiveData = MutableLiveData<HttpServiceResponse<OrderListResponse>>()

    /**
     *获取订单列表tab
     * */
    fun orderListTab() {
        val listener = ViewModelSubscribe<OrderTabResponse>(orderTabLiveData)
        val disposable: Disposable = orderManager.orderListTab(listener)
        dispose.add(disposable)
    }

    /**
     * 订单列表接任务
     * */
    fun orderListTask() {

        AMapLocationUtils.instance()?.locationDataLiveData?.value?.let {
            val lat: String? = it["lat"]
            val lng: String? = it["lng"]
            if (lat.isNullOrEmpty() || lng.isNullOrEmpty())
                return@let
            val request = SocketRequest(
                action = SocketAction.SEND_ORDER_LIST.desc,
                lat = lat, lng = lng
            )
            this.sendSocket(request)
        }
    }

    /**
     * 订单列表待取货
     */
    fun orderListPickUp() {
        AMapLocationUtils.instance()?.locationDataLiveData?.value?.let {
            val lat: String? = it["lat"]
            val lng: String? = it["lng"]
            if (lat.isNullOrEmpty() || lng.isNullOrEmpty())
                return@let
            val listener = ViewModelSubscribe<OrderListResponse>(orderPickupLiveData)
            val disposable: Disposable = orderManager.orderList(lat, lng, ("2"), listener)
            dispose.add(disposable)
        }
    }

    /**
     * 订单列表待送达
     */
    fun orderListGive() {
        AMapLocationUtils.instance()?.locationDataLiveData?.value?.let {
            val lat: String? = it["lat"]
            val lng: String? = it["lng"]
            if (lat.isNullOrEmpty() || lng.isNullOrEmpty())
                return@let
            val listener = ViewModelSubscribe<OrderListResponse>(orderGiveLiveData)
            val disposable: Disposable = orderManager.orderList(lat, lng, ("3"), listener)
            dispose.add(disposable)
        }
    }

    /**
     * 接单
     * */
    fun orderReceiving(riderOrderId: String) {
        val listener = object : ViewModelSubscribe<HttpServiceData>(baseReceivingLiveData) {}
        val disposable: Disposable = orderManager.orderReceiving(riderOrderId, listener)
        dispose.add(disposable)
    }

    /**
     * 取貨
     * */
    fun orderWithdrawal(riderOrderId: String,orderSn:String) {
        val listener = object : ViewModelSubscribe<HttpServiceData>(baseWithdrawalLiveData) {}
        val disposable: Disposable = orderManager.orderWithdrawal(riderOrderId,orderSn, listener)
        dispose.add(disposable)
    }

    /**
     * 送达
     * */
    fun orderService(riderOrderId: String,images:String) {
        val listener = object : ViewModelSubscribe<HttpServiceData>(baseServiceLiveData) {}
        val disposable: Disposable = orderManager.orderService(riderOrderId,images, listener)
        dispose.add(disposable)
    }

    /**
     * 联系电话
     * */
    fun callPhone(phone: String) {
        baseCallPhoneLiveData.postValue(phone)
    }

    /**
     * 发送socket
     * */
    private fun sendSocket(text: SocketRequest) {
        HttpWebSocketUtils.instance()?.send(GsonUtils.toJson(text))
    }

    /**
     * 启动定位
     * */
    fun startLocation() {
        AMapLocationUtils.instance()?.startLocation()
    }

    /**
     * 启动订单刷新
     * */
    fun startOrderRefresh() {
        val disposable: Disposable = Observable.interval((1), (10), TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                orderListTask()
            }
        dispose.add(disposable)

    }

    /**
     * 发送定位
     * */
    fun sendLocation(locationMaps: MutableMap<String, String>) {
        val lat: String? = locationMaps["lat"]
        val lng: String? = locationMaps["lng"]
        if (lat.isNullOrEmpty() || lng.isNullOrEmpty())
            return
        val request = SocketRequest(
            action = SocketAction.SET_RIDER_COORDINATE.desc,
            lat = lat, lng = lng
        )

        this.sendSocket(request)
    }

    enum class SocketAction(val desc: String) {
        SEND_ORDER_LIST("send_order_list"),//取订单数据标识
        SET_RIDER_COORDINATE("set_rider_coordinate")//上报定位标识
    }

}